﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using LumiSoft.Net;
using LumiSoft.Net.POP3.Client;
using LumiSoft.Net.Mime;

namespace Mail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //send mail here
        private void button1_Click(object sender, EventArgs e)
        {
            SmtpClient mailClient = new SmtpClient(tbMailServer.Text);
            mailClient.Credentials = new NetworkCredential(tbUser.Text, tbCredential.Text);
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress("xtsw@baosight.com");
                SetAddressCollection(tbTo.Text, message.To);
                SetAddressCollection(tbCC.Text,message.CC);
                SetAddressCollection(tbBcc.Text,message.Bcc);
                message.Subject = tbSubject.Text;
                message.Body = tbBody.Text;
                //attachment:
                if (!String.IsNullOrEmpty(tbAttachment.Text))
                {
                    message.Attachments.Add(new Attachment(tbAttachment.Text));
                }

                //if the message is too large
                mailClient.Timeout = 60 * 1000 * 5;
                mailClient.Send(message);
            }
            MessageBox.Show("Mail Sent");
        }


        private void SetAddressCollection(string addressList , MailAddressCollection collection)
        {
            if (String.IsNullOrEmpty(addressList))
            {
                return;
            }
            foreach (string address in addressList.Split(',', ';'))
            {
                collection.Add(address);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (attachmentDialog.ShowDialog() == DialogResult.OK)
            {
                tbAttachment.Text = attachmentDialog.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            using (POP3_Client pop3Client = new POP3_Client())
            {
                pop3Client.Connect(tbPopServer.Text, WellKnownPorts.POP3);

                pop3Client.Authenticate(tbPopUser.Text, tbPopCredential.Text, false);

                var mails = from POP3_ClientMessage message in pop3Client.Messages 
                            let mime = Mime.Parse(message.MessageToByte())
                            select new MyMailMessage{ Subject= mime.MainEntity.Subject , Body= mime.BodyText , From = mime.MainEntity.From.ToAddressListString(), MessageId=message.UID} ;
                this.dataGridView1.DataSource = mails.ToList();
            }

        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            System.Windows.Forms.DataGridView.HitTestInfo hti = dataGridView1.HitTest(e.X,e.Y);
            if( hti.ColumnIndex >=0 && hti.RowIndex >= 0)
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Selected = false;
                }
                dataGridView1.Rows[hti.RowIndex].Selected = true;
            }
            
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<string> mailToDelete = null;
            if (dataGridView1.SelectedRows.Count > 0)
            {
                mailToDelete = new List<string>();
                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                    mailToDelete.Add(row.Cells[3].Value.ToString());
                }
            }
            else
            {
                return;
            }
            using (POP3_Client pop3Client = new POP3_Client())
            {
                pop3Client.Connect(tbPopServer.Text, WellKnownPorts.POP3);

                pop3Client.Authenticate(tbPopUser.Text, tbPopCredential.Text, false);

                foreach (POP3_ClientMessage message in pop3Client.Messages)
                {
                    if (mailToDelete.Contains(message.UID))
                    {
                        message.MarkForDeletion();
                    }
                }
            }
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                row.Visible = false;
            }            
        }

    }
}
